home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ASME's Mechanical Engine…ing Toolkit 1997 December
/
ASME's Mechanical Engineering Toolkit 1997 December.iso
/
ai
/
prlg195b.lzh
/
SAMPLES.LZH
/
LISTS.PRO
< prev
next >
Wrap
Text File
|
1987-11-11
|
4KB
|
131 lines
/* ------------------------------------------------------------------
LISTS.PRO by Eugene L. Frassetto
------------------------------------------------------------------
This file contains a collection of list manipulation
predicates. While you may find theme useful in your
programs, they are most usefull in demonstrating the
recursive nature of prolog and a general way in which
lists may be manipulated in a prolog program.
-----------------------------------------------------
predicates in the file:
is_list last_of( list, X )
length_of append( list1, list2, X )
head_of member_of ( list, X )
second_of reverse( list, X )
tail_of sum_of( list, X )
nth_of dif_of( list, X )
----------------------------------------------------------------------- */
/* -----------------------------------------------
test if a variable is a list
is_list( [list] ). => yes or no.
-------------------------------------------------- */
is_list( [] ).
is_list( [Head | Tail] ) :- is_list( Tail ).
/* ----------------------------------
/* get the length of a list
------------------------------------- */
length_of( [], 0 ).
length_of( [_ | Tail], Length ) :-
NewLen is Length,
length_of( Tail, NewLen ), Length is NewLen + 1.
/* -------------------------------------------------------
get the head or second element or the tail of a list
head_of( [list], X ). => X = list-head.
second_of( [list], X ). => X = list-element-2.
tail_of( [list], X ). => X = list-tail.
---------------------------------------------------------- */
head_of( [Head | Tail], Head).
second_of( [_, Second | Tail], Second ).
tail_of( [Head | Tail], Tail).
/* ------------------------------------------------------
get the nth element of a list (0 based)
nth_of( [], anynum, X ). => [].
nth_of( [a,b,c], 1, X ). => b.
nth_of( [a,b,c], 4, X ). => [].
---------------------------------------------------------- */
nth_of( [], N, [] ).
nth_of( [H | T], 0, H ).
nth_of( [H | T], N, X ) :- C is N - 1, nth_of( T, C, X).
/* --------------------------------------------------
get the last element of a list
last_of( [list], X ). => X = last-element.
------------------------------------------------------- */
last_of( [X], X ).
last_of( [_ | T], X) :- last_of( T, X).
/* --------------------------------------------------------
append to a list
append( [list-1], [list-2], X). => X = [list-1, list-2].
NOTE: entering append(X,[d,e],[a,b,c,d,e]) => X = [a,b,c].
append([a,b],X,[a,b,c,d,e]) => X = [c,d,e].
---------------------------------------------------------- */
append( [], AnyList, AnyList).
append( [Head | Tail], List2, [Head | NewTail] ) :-
append( Tail, List2, NewTail ).
/* --------------------------------------------------------
check if an element of a list
member_of( [list], what ). => yes or no.
member_of( [list], X ). => X = first, X = second, . . .
----------------------------------------------------------- */
member_of( [Element | _], Element ).
member_of( [_ | Tail], Element ) :- member_of( Tail, Element ).
/* -------------------------------------------------
reverse the order of the elements of a list
reverse([a,b,c,d],X). => X = [d,c,b,a].
reverse([a,b,c],[c,b,a]). => Yes.
---------------------------------------------------- */
reverse([F], [F]).
reverse([H | T], X) :-
reverse(T, Y),
append(Y, [H], X).
/* ------------------------------------------------
add a list of numbers
sum_of([2],X). => X = 2.
sum_of([1,2,3,4,5],X). => X = 15.
sum_of([1,2,3],6). => Yes.
sum_of([1,2,3],10). => No.
---------------------------------------------------- */
sum_of( [X], X).
sum_of( [F, S | T], X) :-
R is F + S,
sum_of( [R | T], X).
/* ------------------------------------------------
subtract a list of numbers
dif_of([2],X). => X = 2.
dif_of([10,5],X). => X = 5.
dif_of([1,2,3,4,5],X). => X = -13.
dif_of([1,2,3],-4). => Yes.
dif_of([1,2,3],-10). => No.
---------------------------------------------------- */
dif_of( [X], X).
dif_of( [F, S | T], X) :-
R is F - S,
dif_of( [R | T], X).